/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package edu.pku.sei.modeler.uml.test;

import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;

import edu.pku.sei.modeler.figures.CombinedFragmentFigure;
import edu.pku.sei.modeler.figures.InteractionFigure;
import edu.pku.sei.modeler.figures.InteractionUseFigure;

public class FragmentBorder extends AbstractBorder {
	// protected int size;
	private static final int TOP_INSET = 4;
	private static final int LEFT_INSET = 6;
	private static final int BOTTOM_INSET = 4;
	private static final int RIGHT_INSET = 6;

	private Color color = ColorConstants.black;
	private Color backgroundColor = ColorConstants.white;

	protected static Insets insets = new Insets(TOP_INSET, LEFT_INSET,
			BOTTOM_INSET, RIGHT_INSET);

	public FragmentBorder() {
		super();
	}

	public Insets getInsets(IFigure figure) {
		return insets;
	}

	public void paint(IFigure figure, Graphics g, Insets in) {
		final Rectangle r = figure.getBounds().getCropped(in);
		g.setForegroundColor(this.color);
		g.setBackgroundColor(this.backgroundColor);
		g.drawRectangle(r.x, r.y, r.width - 1, r.height - 1);
		Rectangle tr = getLabelBounds(figure);
		final int s = tr.height / 2; // size of the corner diagonal line
		final int right = r.x + tr.right() + LEFT_INSET;
		final int bottom = r.y + tr.bottom() + 2 * TOP_INSET;
		g.drawLine(r.x, bottom, right, bottom);
		g.drawLine(right, bottom, right + s, bottom - s);
		g.drawLine(right + s, bottom - s, right + s, r.y);
	}

	// TODO Introduce an interface here that describes the relationship
	// between the border and the bordered figure

	public void paintLabelBackground(IFigure figure, Graphics g) {
		Rectangle r = figure.getBounds();
		Rectangle tr = getLabelBounds(figure);
		final int s = tr.height / 2; // size of the corner diagonal line
		final int right = r.x + tr.right() + LEFT_INSET;
		final int bottom = r.y + tr.bottom() + 2 * TOP_INSET;
		g.setForegroundColor(this.color);
		g.setBackgroundColor(this.backgroundColor);
		g.fillPolygon(new int[] { r.x + 1, r.y + 1, r.x + 1, bottom, right,
				bottom, right + s, bottom - s, right + s, r.y + 1 });
		g.restoreState();
		
	}

	private Rectangle getLabelBounds(final IFigure figure) {
		if (figure instanceof InteractionFigure) {
			return((InteractionFigure) figure).getLocalTextBounds();
		}else if (figure instanceof InteractionUseFigure) {
			return ((InteractionUseFigure)figure).getLocalTextBounds();
		}
		else if (figure instanceof CombinedFragmentFigure) {
			return ((CombinedFragmentFigure)figure).getLocalTextBounds();
		}
		else {
			//TODO why to return null? Chengnian Sun.
			throw new RuntimeException("Unhandled figure type in FragmentBorder. " + figure.getClass().getCanonicalName());
		}
//		else if (figure instanceof InteractionUseFigure)
//			tr = ((InteractionUseFigure) figure).getLocalTextBounds();
//		else
//			tr = ((CombinedFragmentFigure) figure).getLocalTextBounds();
	}

	/**
	 * Returns the height of the label "box" in the top left corner.
	 */
	public int getLabelHeight(IFigure figure) {
		return getLabelBounds(figure).height + 2 * TOP_INSET;
	}

	/**
	 * Sets the color.
	 * 
	 * @param color
	 *            the color to set
	 */
	public void setColor(Color color) {
		if (color != null) {
			this.color = color;
		}
	}

	public void setBackgroundColor(Color backgroundColor) {
		if (backgroundColor != null) {
			this.backgroundColor = backgroundColor;
		}
	}
	
	public Dimension getPreferredSize(IFigure f) {
		return new Dimension(200, 100);
	}
}